<?php
    if(file_exists('../../../../definitions.inc.php')) include_once('../../../../definitions.inc.php');
    require_once('../../../functs.inc');
    require_once('../../../funct.inc');
    include('../../../session.inc');
    if(!GetAdminID($ID)){echo LOGIN_ERR;exit;}
    if(!Right_Check(2)){header('Location:error.php');exit;}
    include '../../../color_style.php';
    include '../../../'.LANGUAGE;
    require_once('../../../../classDB.inc.php');
    
 do{
    $_DB = new classDB();
    if (!$_DB->open()){echo OPEN_DB_ERR; break;}

    $OrderID=$_REQUEST["ID"];
    print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">";
    print "<html>";
    print "<head>";
    echo '<meta http-equiv="Content-Type" content="text/html; charset='._CHARSET_.';">';
    print "<title></title>";

    include '../../../html_style.php';

    print "</head>";
    print "<body style=\"background-color:white;\">";

        print "<table cellpadding=\"0\" cellspacing=\"0\"  style=\"font-size:10px; width:650px; border:1px solid black;\">";
        @$dbResult=mysql_query("SELECT * FROM invoice_heads_archive WHERE ID=".$OrderID);
        if (!dbResult) {
            print INV_CHYBA_PRI_VYHLADAVANI_UDAJOV_STRANKU_NEMOZNO_ZOBRAZIT;
            break;
        }
        $sql_array_User = mysql_fetch_object($dbResult);
        //**********************************************************************
        //zobrazenie udajov o dodavatelovi
        //**********************************************************************
        print "<tr style=\"border:1px solid black;\"><td valign=\"top\"  style=\"border:1px solid black;\">";
            print ("<b><u>".INV_DODAVATEL.":</u></b><br />");
            $file = fopen("../../../address.txt", "r") or die (INV_CHYBA_SUBORU." 'address.txt'!");
            while (!feof($file)){
                    $file_row = fgets($file, 500);
                    print ($file_row. "<br />");
            }            
        
            //sposob dorucenia
            print "<br /><br />".INV_SPOSOB_DORUCENIA.":".$sql_array_User->IHA_Delivery;
            
        print "</td>";
        //**********************************************************************
        //zobrazenie hlavicky faktury a udajov o odberatelovi
        //**********************************************************************
        print "<td valign=\"top\">";
        	print "<table style=\"width:100%; font-size:10px;\">";
				print "<tr>";
					print "<td style=\"border-bottom:1px solid black;\">";
						print "
							<table style=\"font-size:10px;\">
								<tr>
									<td>
										<b>".INV_FAKTURA_CISLO.":</b>
									</td>
									<td>
										<b>".$sql_array_User->IHA_Prefix.$sql_array_User->IHA_No."</b>
									</td>
								</tr>
								<tr>
									<td>
										".INV_KONSTANTNY_SYMBOL.":
									</td>
									<td>
										".$sql_array_User->IHA_Constant_Symbol."
									</td>
								</tr>
							</table>
						";
					print "</td>"; 
				print "</tr>";
				print "<tr>";
					print "<td>";                                	
                        print "<b><u>".INV_ODBERATEL.":</u></b>";
                        if ($sql_array_User->IHA_ICO!="")
                            print "(".INV_ICO.": ".$sql_array_User->IHA_ICO.")<br />";
                        else
                            print "<br />";
                        //vyhladanie udajov v DB
                        if ($sql_array_User->IHA_Organisation!="")
                            print $sql_array_User->IHA_Organisation."<br />";
                        if ($sql_array_User->IHA_Organisation=="")
                        	print $sql_array_User->IHA_Name." ".$sql_array_User->IHA_Surname."<br />";
                        print $sql_array_User->IHA_Street."<br />";
                        print $sql_array_User->IHA_PostID." ".$sql_array_User->IHA_City."<br />";		                                        
                        if ($sql_array_User->IHA_DIC!="")
                            print INV_DIC.": ".$sql_array_User->IHA_DIC."<br />";
                        $Order_text = $sql_array_User->IHA_Additorial_Text;
                        mysql_free_result($dbResult);
            		print "</td>"; 
				print "</tr>"; 
				print "<tr>";
					print "<td style=\"border-top:1px solid black;\">";
						print "
							<table style=\"font-size:10px;\">
								<tr>
									<td>
										".INV_DATUM_VYSTAVENIA.":
									</td>														
									<td>  
										".date(sttngs_DATE_FORMAT,strtotime($sql_array_User->IHA_Create))."
									</td>
								</tr>
								<tr>
									<td>
										".INV_DATUM_SPLATNOSTI.":
									</td>
									<td>  
										".date(sttngs_DATE_FORMAT,strtotime($sql_array_User->IHA_Expiration))."
									</td>
								</tr>
								<tr>
									<td>
										".INV_DATUM_DAN.":
									</td>
									<td>  
										".date(sttngs_DATE_FORMAT,strtotime($sql_array_User->IHA_Tax))."
									</td>
								</tr>
								<tr>
									<td>
										".INV_SPOSOB_PLATBY.":
									</td>
									<td>  
										".$sql_array_User->IHA_Payment_Type."
									</td>
								</tr>
							</table>
						";
					print "</td>"; 
				print "</tr>";          
            print "</table>";
        print "</td></tr>";
        //**********************************************************************
        //zobrazenie tovaru
        //**********************************************************************
        if(_PRODUCT_CODE_SHOWING_=='YES') $CodeCaption = "(".O_KOD.") "; else $CodeCaption = "";

        print "<tr><td colspan=\"2\">";
            print ("<b><u>".INV_FAKTURUJEME_VAM_ZA.":</u></b></br>");
        print "</td></tr>";
        print "<tr><td colspan=\"2\" style=\"border-bottom:1px solid black;\">";
        print "<table width=\"100%\" style=\"font-size:10px;\">";
        
        if(PAYER_VAT=="YES")
            //**********************************************************************
            //platca DPH
            print "<tr><td style=\"width:220px;margin-right:10px;\"><b>".$CodeCaption.O_POLOZKA."</b></td><td><b>".O_MJ."</b></td><td><b>".O_CENA_ZA_MJ."</b></td><td><b>".O_DPH."</b></td><td><b>".O_CENA_S_DPH."</b></td><td><b>".O_MNOZSTVO."</b></td><td><b>".O_SPOLU."</b></td></tr>";
            //**********************************************************************
            else
            //**********************************************************************
            //platca DPH
            print "<tr><td style=\"width:270px;margin-right:10px;\"><b>".$CodeCaption.O_POLOZKA."</b></td><td><b>".O_MJ."</b></td><td><b>".O_CENA_ZA_MJ."</b></td><td><b>".O_MNOZSTVO."</b></td><td><b>".O_SPOLU."</b></td></tr>";
            //**********************************************************************        
        $TotalSum=0;
        $sql_bin= "SELECT * FROM invoice_products_archive WHERE IHA_ID=".$OrderID;
        @$bin_source = mysql_query($sql_bin);
        if (!bin_source) {
            print INV_CHYBA_PRI_VYHLADAVANI_UDAJOV_STRANKU_NEMOZNO_ZOBRAZIT;
            break;
        }
        while ($sql_array_Prodct = mysql_fetch_object($bin_source))
        {
            print "<tr><td>";
                if(_PRODUCT_CODE_SHOWING_=='YES') print "(".$sql_array_Prodct->IPA_Code.") ".$sql_array_Prodct->IPA_Name;
		  else $sql_array_Prodct->IPA_Name;
                print "</td>";
                print "<td>";
                print $sql_array_Prodct->IPA_UnitType;
                print "</td>";
                print "<td>";
                print FormatItAsCurrency($sql_array_Prodct->IPA_Price);
                print "</td>";
                
                if(PAYER_VAT=="YES"){
                    //**********************************************************************
                    //platca DPH
                    print "<td>";
                            print $sql_array_Prodct->IPA_DPH;
                    print "</td>";
                    print "<td>";
                            print FormatItAsCurrency(((($sql_array_Prodct->IPA_DPH)/100)+1) * $sql_array_Prodct->IPA_Price);
                    print "</td>";
                    //**********************************************************************
                }
                
                print "<td>";
                print $sql_array_Prodct->IPA_Count;
                print "</td>";
                
                
                if(PAYER_VAT=="YES"){
                //**********************************************************************
				//platca DPH
                	print "<td>";
                	print FormatItAsCurrency(((($sql_array_Prodct->IPA_DPH)/100)+1) * $sql_array_Prodct->IPA_Price * $sql_array_Prodct->IPA_Count);
                	print "</td>";
                }
                else{
                  	//neplatca DPH
                	print "<td>";
                	print FormatItAsCurrency($sql_array_Prodct->IPA_Price * $sql_array_Prodct->IPA_Count);
                	print "</td>";
                }
                
                
            print "</tr>";
            
            if(PAYER_VAT=="YES"){
                //**********************************************************************
				//platca DPH
                $TotalSum+=((($sql_array_Prodct->IPA_DPH)/100)+1) * $sql_array_Prodct->IPA_Price * $sql_array_Prodct->IPA_Count;
            }
            else{
            	//neplatca DPH
            	$TotalSum+= $sql_array_Prodct->IPA_Price * $sql_array_Prodct->IPA_Count;
            }
        }

        print "</table>";
        print "</td></tr>";
        //**********************************************************************
        //prepocet rozpisu DPH
        //**********************************************************************
        if(PAYER_VAT=="YES"){ //iba platca DPH
            $aiVATLevels[-1] = 0;
            $sql_bin= "SELECT * FROM invoice_products_archive WHERE IHA_ID=".$OrderID;
            $bin_source = mysql_query($sql_bin);
            while ($sql_array_Prodct = mysql_fetch_object($bin_source))
            {
                if(array_key_exists($sql_array_Prodct->IPA_DPH, $aiVATLevels)){
                    $aiVATLevels[$sql_array_Prodct->IPA_DPH]+= $sql_array_Prodct->IPA_Price * $sql_array_Prodct->IPA_Count;
                }else{
                    $aiVATLevels[$sql_array_Prodct->IPA_DPH] = $sql_array_Prodct->IPA_Price * $sql_array_Prodct->IPA_Count;
                }
            }
        }
        //**********************************************************************
        //zobrazenie celkovej sumy
        //**********************************************************************
        print "
			<tr>
				<td style=\"text-align:left; width:250px;\">
					<br />
					$Order_text
					<br />
					<br />
					<br />
					<br />
					<br />
				</td>
				<td style=\"text-align:right;\">
		";
                	if(PAYER_VAT=="YES"){
                    	//**********************************************************************
    					//platca DPH
                        if(count($aiVATLevels)>1){
                            print '
                                <table style="font-size:10px; border:1px dotted black;">
                                    <tr>
                                        <td>
                                            <b>'.INV_SADZBA.'</b>
                                        </td>
                                        <td>
                                            <b>'.INV_ZAKLAD_DANE.'</b>
                                        </td>
                                        <td>
                                            <b>'.O_DPH.'</b>
                                        </td>
                                        <td>
                                            <b>'.O_SPOLU.'</b>
                                        </td>
                                    </tr>';

                            foreach($aiVATLevels as $key=>$value){
                                if($key>-1) print '
                                    <tr>
                                        <td>
                                            '.$key.'%
                                        </td>
                                        <td>
                                            '.RoundIt($value).' '.O_MENA.'
                                        </td>
                                        <td>
                                            '.RoundIt($value*($key/100)).' '.O_MENA.'
                                        </td>
                                        <td>
                                            '.RoundIt($value*(1+($key/100))).' '.O_MENA.'
                                        </td>
                                    </tr>
                                ';
                            }

                            print '</table>';
                        }

                        print "<br /><b><u>".O_CELKOM.":".RoundIt($TotalSum)." ".O_MENA_S_DPH."</u></b>";
                        print '<br>';
                	}
                    else{
                    	//neplatca DPH
                    	print "<br /><b><u>".O_CELKOM.":".RoundIt($TotalSum).O_MENA."</u></b><br />";
                        print '<br><br>';
                        print O_NIE_SME_PLATCOVIA_DPH."<br />";
                    }
        print "
				</td>
			</tr>
		</table>
		";
	        
        //print "</table>";
        mysql_close();
 }while(false);

?>

<script type="text/javascript">
<!--
        window.print();
-->
</script>


</body>

</html>
